<?php

class caiji_class{

private $path='caiji'; //当前模块目录
private $dbver=100; //当前模块的数据库版本

public function __construct(){
	global $king;

	$dbver=(int)$king->getModuleVer($this->path);
	if($this->dbver > $dbver){
		if($this->install_update($dbver)){//成功则返回True，则更新数据库中的版本信息
			$array=array(
				'ndbver'=>$this->dbver,
			);
			$king->db->update('%s_module',$array,"kpath='{$this->path}'");
		}
	}
}

/* ------>>> 安装部分 <<<---------------------------- */

public function install(){
	global $king;

	$this->install_update(100);

	//写模块安装记录
	if(!$king->db->getRows_one('SELECT * FROM %s_module where kpath=\''.$this->path.'\';')){
		$_array=array(
			'kname' =>$king->lang->get($this->path.'/name'),
			'kpath' =>$this->path,
			'kdb'   =>'',
			'ndate' =>time(),
			'ndbver'=>$this->dbver,
			'norder'=>$king->db->neworder('%s_module'),
			'nshow'=>0,
			);
		$kid1=$king->db->insert("%s_module",$_array);


		//添加采集列表
		$sql1='listid int(10) not null,url char(200) not null,nshow int(10) not null,UNIQUE(url)';

		$king->db->createTable('%s_caiji_list',$sql1,'kid');

	








		return true;
	}else{
		return false;
	}
}

public function install_update($ver){

	return True;
}


/**
菜单调用
*/
public function inc_menu($a){
	global $king;

	$left=array(
		'2'=>array(
			'href'=>'manage.php',
			'ico'=>'p7',
			'title'=>'模块首页',
			),
		'3'=>array(
			'href'=>'../portal/manage.content.php?action=edt&listid='.$a,
			'ico'=>'p7',
			'title'=>'添加采集',
			),
	);
	return array($left,array());
}





//采集列表函数
public function caiji_list($array,$bs=''){
	global $king;
		
		
	require_once 'class_ciji.php';/*必须的*/
	require_once 'Snoopy.class.php';/*必须的*/	
	$spider  = new spider();
	$spider->kuiurl = $array['k_lujing'];
	$spider->bm=$array['k_bm'];	
	$spider->getContent();
	$spider->kuibody=explode('||',$array['k_body']);
	$a=$spider->getbody();

	if(strlen($a)!=$array['nnumber'] || !empty($bs)){

		$arraly=array('#'.$array['k_urlzz'].'#Us',$array['k_urlwz']);
		$kuibody=$spider->gettitle($arraly[0],$arraly[1]);//标题
		
	
		if(!empty($bs)){
			foreach($kuibody as $key => $val){
				$kuissss[]=str_replace("(nr)",$val,$array['k_urlbq']);
				}
			return 	$kuissss;
		}
			
			
			foreach($kuibody as $key => $val){
				$url=str_replace("(nr)",$val,$array['k_urlbq']);
					if(!$ressd = $king->db->getRows_one("select url from %s_caiji_list where url = '".$url."'")){

						$_array_cs=array(
							'listid' => $array['kid'],
							
							'url'=>$url,
						);
						$cr=$king->db->insert("%s_caiji_list",$_array_cs);
					}
			}

			$asr=array('nnumber' => strlen($a));
			
			$king->db->update('%s__caijimk',$asr,"kid=".$array['kid']);
			$king->cache->del('portal/caiji/list');
			

			return 	$cr;	
	}

}










//内容入库
public function caiji_index($arrayki,$bs=''){
	global $king;

	$listid=$arrayki['listid'];
	$ktitle=$arrayki['ktitle'];
	$ndate=strtotime(date("Y-m-d H:i:s"));

	$kcontent=kc_grab($arrayki['kcontent']);
	$kpath=caiji_class::randStr(10);
	$tag=$arrayki['tag'];
	$dec=$arrayki['dec'];
	$keywords=$arrayki['keywords'];

	if(empty($dec) && !empty($kcontent)){
		$kdescription=strip_tags($kcontent);
		$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
		$dec=kc_substr($kdescription,0,200);
	}


  	if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$kcontent,$oneimage_array)){
					$images=$oneimage_array[5];
					
	}
	
					
				if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$kcontent,$oneimage_array)){
					$smartimg=$oneimage_array[5];					
					$smartimg=str_replace('/browse/','',$smartimg);			
				}
	
			
				$kcontent=preg_replace('/<a ([^>]*)>|<\/a>/is','',$kcontent);							
				$kcontent=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe|div) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe|div)>/is','',$kcontent);												
				$kcontent=preg_replace('/(<([^>]*))( style=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$kcontent);											
				$kcontent=preg_replace('/(<([^>]*))( id=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$kcontent);													
				$kcontent=preg_replace('/(<([^>]*))( class=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$kcontent);
	
	
	
	
	
	
	

    if(!empty($ktitle)){
	
		if(empty($tag)){
			//$tag = keywords_class::k_dangdang_fc($ktitle);
		}
		$_array=array(
			'listid' =>$listid,
			'ktitle' =>$ktitle,
			'nlastdate'  =>$ndate,
			'ndate'   =>$ndate,
			'kcontent' =>$kcontent,
			'kkeywords'=>$keywords,
			'kpath'=>'page-'.$listid.'-'.$kpath.'.html',
			'ktag'=>$tag,			
			'kdescription'=>$dec,
			'kimage'=>$smartimg,
			'nshow'=>'0',
			);

		$_array['k_author']=' ';
		if(!empty($bs)){return $_array;}
		if(!$res=$king->db->getRows_one("select * from %s__article where ktitle='".$ktitle."';")){
			$a=$king->db->insert("%s__article",$_array);
		}else{
		$a="重复";
		}
			$asr=array('nshow' => 1);
		$king->db->update('%s_caiji_list',$asr,"kid=".$arrayki['kuiid']);

		return $a; 
	}
}


public function randStr($len) 
{ 
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; // characters to build the password from 
$string=''; 
for(;$len>=1;$len--) 
{ 
$position=rand()%strlen($chars); 
$string.=substr($chars,$position,1); 
} 
return $string; 
} 












public function caiji_content($array,$ad,$bs=''){
			//内容采集模块
			if(empty($array)){return  $ary;}
			global $king;
			///////////////////////////////////////////////////////////////////////
			//将内容进行组合生成适合采集格式的数组
			$ary['url']=$array['url'];	
		
			foreach($ad as $key => $val){	
		
				if($val['kid'] == $array['listid']){
			
						$ary['k_bm']=$val[k_bm];
						$ary['ksjs']=explode("||",$val[k_neirrongquyu]);
						$ary['k_neirongzz']=caiji_class::caiji_array(explode("\n",$val['k_neirongzz']),'|');
						$ary['k_neironggl']=caiji_class::caiji_array(explode("\n",$val['k_neironggl']),'|');
						$ary['k_rkgz']=caiji_class::caiji_array(explode("\n",$val['k_rkgz']),'|');
						$ary['k_tih']=caiji_class::caiji_array(explode("\n",$val['k_tih']),'|');
				}
				
			}	
			
				

			//对数组进行采集
				require_once 'class_ciji.php';/*必须的*/
				require_once 'Snoopy.class.php';/*必须的*/				
				$spider  = new spider();
				$spider->kuiurl = $ary['url'];
		
				$spider->bm=$ary['k_bm'];	
				$spider->getContent();				
				$spider->kuibody=$ary['ksjs'];
				$a=$spider->getbody();
		
				if(!empty($a)){//如果内容不为进行采集过滤
					$kuibodsyd['kuiid']=$array['kid'];	
					if(!empty($ary[k_neironggl])){						
					foreach($ary[k_neironggl] as $key => $vals){													
						$kuigl[$vals[0]]=explode('%g%',$vals[1]);;
					}	
					}
					if(!empty($ary[k_tih])){				
						foreach($ary[k_tih] as $key => $valt){													
							$kuith[$valt[0]]=caiji_class::caiji_array(explode('%g%',$valt[1]),'%t%');
						}	
					}
					if(!empty($ary[k_neirongzz])){
						foreach($ary[k_neirongzz] as $key => $val){						
							$arraly[$val[0]]=array($val[1],ceil($val[2]));						
							$kuibd[$val[0]]=$spider->gettitle($arraly[$val[0]][0],$arraly[$val[0]][1]);//标题						
							$kuibody[$val[0]]=caiji_class::caiji_ret($kuibd[$val[0]][0],$kuith[$val[0]],$kuigl[$val[0]]);
						}	
					}	
				}

				foreach($ary['k_rkgz'] as $key => $valsd){	
					
					if($valsd[0]=='listid'){
						$kuibodsyd['listid'] = $valsd[1];
					}else{
						$a=addslashes($valsd[1]);
						$a=preg_replace('/(\&[a-z]{1,6};)|\s/','',$a);	
						$kuibodsyd[$valsd[0]]=$kuibody[$a];
					}												
				}

				if(!empty($kuibodsyd)){				
					$sd=caiji_class::caiji_index($kuibodsyd,$bs);
				}
				if(!empty($bs)){return $sd;}
				if(!empty($sd)){
					return  '1';
				}			
		}


public function caiji_array($array,$n){
		//拆分字符串生成数组		
		foreach($array as $key => $val){
			if(!empty($val)){
				$ary[]=explode($n,$val);
			}
		}
		return $ary;
}

public function caiji_ret($n,$th,$gl){
		//拆分字符串生成数组	
		if(!empty($th)){
			foreach($th as $key => $val){
				if(!empty($val)){

						$n=str_replace($val[0],$val[1],$n);
				}
			}
		}
		if(!empty($gl)){

			foreach($gl as $key => $val){
			
				if(!empty($val)){
					
						//$n=str_replace($val[0],'',$n);
						$as="'".$val."'si";
					//	$n=preg_replace('[(<div>)+.+</div>]',"",$n);
						$n=preg_replace($as,"",$n);
				}
			}
		}
	//	print_r($n);
		return $n;
}







public function caiji_edt(){
	global $king;

$test.='采集程序<br>';
$test.='采集开始>>>>>>>>>>>>>>><br><br>';


		if(!$ad=$king->cache->get('portal/caiji/list',time()-86400)){
$test.='↓生成采集规则缓存到caiji/list目录↓<br><br>';

				$ad=$king->db->getRows("select kid,k_lujing,k_body,k_urlzz,k_bm,k_urlwz,k_urlbq,nnumber,nprice,k_neirongzz,k_neironggl,k_rkgz,k_tih,k_neirrongquyu from %s__caijimk where nshow='1'");					
				foreach($ad as $key => $val){
					$ads[$val['kid']]=$val;
				}		
				$king->cache->put('portal/caiji/list',$ads);			
		}	
		
		
$test.='↓获取采集列表↓<br>';

		//判断采集缓存文件是否存在,如果不存在进行采集列表
		foreach($ad as $key => $val){		
			if(!$king->cache->get('portal/caiji/list/'.$val['kid'],time()-$val['nprice']*60)){	
			
				$test.='↓采集'.$val['kid'].'列表↓<br>';	
	
				$a=caiji_class::caiji_list($val);	//采集列表函数				
				$king->cache->put('portal/caiji/list/'.$val['kid'],'1');//写入列表的缓存			
			}
			
	}

			if(empty($a)){
				$test.='↓采集内容↓<br>';	

				if(!$nrlist=$king->cache->get('portal/caiji/list/neironglist')){				
	
					$nrlist=$king->db->getRows("select * from %s_caiji_list where nshow = 0 limit 100");
					$king->cache->put('portal/caiji/list/neironglist',$nrlist);//写入列表的缓存
											
				}
	
				$kuis=caiji_class::caiji_content($nrlist[0],$ad);
				if(!empty($kuis)){
					array_splice($nrlist,0,1);  
					$king->cache->put('portal/caiji/list/neironglist',$nrlist);//写入列表的缓存
				}
				if(empty($nrlist)){
	
						$king->cache->del('portal/caiji/list/neironglist');
							
				}
			}else{
				exit(); 
			}		
	

	
	
	
	
	echo $test;
}



}


?>
